'use strict';

var _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault');
Object.defineProperty(exports, '__esModule', {
  value: true,
});
exports.default = exports.Provider = exports.InternalFormContext = void 0;
var _objectSpread2 = _interopRequireDefault(require('@babel/runtime/helpers/objectSpread2'));
var _slicedToArray2 = _interopRequireDefault(require('@babel/runtime/helpers/slicedToArray'));
var _react = require('react');
var _Form = _interopRequireDefault(require('../../Form'));
var InternalFormContext = /*#__PURE__*/ (0, _react.createContext)(undefined);
exports.InternalFormContext = InternalFormContext;
var Provider = InternalFormContext.Provider;
exports.Provider = Provider;
var useForm = function useForm() {
  var _Form$useForm = _Form.default.useForm(),
    _Form$useForm2 = (0, _slicedToArray2.default)(_Form$useForm, 1),
    form = _Form$useForm2[0];
  var context = (0, _react.useContext)(InternalFormContext);
  var fieldsShouldBindChange =
    (context === null || context === void 0 ? void 0 : context.fieldsShouldBindChange) || [];
  return [
    (0, _objectSpread2.default)(
      (0, _objectSpread2.default)(
        {
          // @ts-ignore
          __powerBySzzjSchemaForm: true,
        },
        form,
      ),
      {},
      {
        setFields: function setFields(flds) {
          form.setFields(flds);
          fieldsShouldBindChange.forEach(function (field) {
            var found = flds.find(function (fe) {
              return fe.name === field.name;
            });
            if (found) {
              field.onChange(found.value);
            }
          });
        },
        /**
         * 赋值联动
         * @param values
         */
        setFieldsValue: function setFieldsValue(values) {
          form.setFieldsValue(values);
          fieldsShouldBindChange.forEach(function (field) {
            if (values[field.name] !== undefined) {
              field.onChange(values[field.name]);
            }
          });
        },
      },
    ),
  ];
};
var _default = useForm;
exports.default = _default;
